// DeviceChooserDialog.cpp : implementation file
//

#include "stdafx.h"

#include "DeviceChooserDialog.h"


// DeviceChooserDialog dialog
// I'm thinking you add or remove devices using this dialog, then you can
// close it, and the destructor can make the correct changes

IMPLEMENT_DYNAMIC(DeviceChooserDialog, CDialog)

DeviceChooserDialog::DeviceChooserDialog(CWnd* pParent /*=NULL*/)
	: CDialog(DeviceChooserDialog::IDD, pParent)
{
}

DeviceChooserDialog::~DeviceChooserDialog()
{
}

void DeviceChooserDialog::SetCommsObject(GPScomm *c)
{
	gps = c;
}

void DeviceChooserDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, availableDevices, availDevices);
	DDX_Control(pDX, usedDevices, devicesToUse);
}

void DeviceChooserDialog::OnOK()
{
	int numPorts = devicesToUse.GetCount();

	if(numPorts == 0)
		return;
	else if(numPorts != LB_ERR)
	{
		delete gps;
		gps = new GPScomm();
		
		LPTSTR dev = new wchar_t[10];
		bool server_connection=false;

		for(int i = 0; i < numPorts; i ++)
		{
			//TODO: IF we ever have something besides COM ports here, we need to
				//do something differently.
			int error = devicesToUse.GetText(i, dev);
			if(error == LB_ERR)
			{
				MessageBox(L"should never see this.", L"asld");
				break;
			}
			//TODO: Find a WCHAR to integer function so that we don't need this switch
			switch(dev[3])
			{
			case (WCHAR)'0':
				gps->useCOM(0);
				break;
			case (WCHAR)'1':
				gps->useCOM(1);
				break;
			case (WCHAR)'2':
				gps->useCOM(2);
				break;
			case (WCHAR)'3':
				gps->useCOM(3);
				break;
			case (WCHAR)'4':
				gps->useCOM(4);
				break;
			}
		}
		gps->Connect();
		//TODO: Add these to a thread dedicated to reading from the GPS
		//We may also need something in the GPScomm class that is capable
		//of reading from all of the used COM ports at once.
		gps->send_binary_mode_string();
		gps->open_output_file();
		gps->server_connection=gps->connect_to_server();
		
		gps->close_output_file();
		delete dev;
	}
	else
	{
		MessageBox(L"Could not get data from Device Picker.", L"Error.");
	}
	this->DestroyWindow();
}

BEGIN_MESSAGE_MAP(DeviceChooserDialog, CDialog)
//	ON_COMMAND(ID_MENU_PICKGPSDEVICES, &DeviceChooserDialog::OnMenuPickgpsdevices)
ON_BN_CLICKED(Refresh, &DeviceChooserDialog::OnBnClickedRefresh)
ON_BN_CLICKED(IDC_BUTTON1, &DeviceChooserDialog::OnBnClickedButton1)
ON_BN_CLICKED(IDC_BUTTON2, &DeviceChooserDialog::OnBnClickedButton2)
ON_LBN_SELCHANGE(availableDevices, &DeviceChooserDialog::OnLbnSelchangeavailabledevices)
END_MESSAGE_MAP()


// DeviceChooserDialog message handlers


void DeviceChooserDialog::OnBnClickedRefresh()
{
	// TODO: Add your control notification handler code here
	availDevices.ResetContent();
	availDevices.AddString(L"COM0");
	availDevices.AddString(L"COM1");
	availDevices.AddString(L"COM2");
	availDevices.AddString(L"COM3");
	availDevices.AddString(L"COM4");
}

void DeviceChooserDialog::OnBnClickedButton1()
{
	// TODO: Add your control notification handler code here
	int index = availDevices.GetCurSel();
	if (index != LB_ERR){
		LPTSTR deviceToAdd;
		availDevices.GetText(index, deviceToAdd);
		devicesToUse.AddString(deviceToAdd);
	}
	else{
		MessageBox(L"Select a device to add (from the top box)");
	}
}

void DeviceChooserDialog::OnBnClickedButton2()
{
	// TODO: Add your control notification handler code here
	int index = devicesToUse.GetCurSel();
	if (index != LB_ERR) {
		devicesToUse.DeleteString(index);
	}
}

void DeviceChooserDialog::OnLbnSelchangeavailabledevices()
{
	// TODO: Add your control notification handler code here
}
